//
//  SimBottomTabBarVC.m
//
//  Created by Xubin Liu on 12-7-11.
//

#import "SimBottomTabBarVC.h"
#import "SimSegmentBar.h"

@interface SimBottomTabBarVC ()
@property (nonatomic, assign) BOOL innerSet;
@end

@implementation SimBottomTabBarVC

- (void)removeTabBarSubview
{
    for (UIView *subview in [self.tabBar subviews]) {
        if (subview != _tabBarView) {
            [subview removeFromSuperview];
        }
    }
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [self removeTabBarSubview];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [self removeTabBarSubview];
}

- (void)setTabBarView:(SimSegmentBar *)tabbarView{
	if (_tabBarView != tabbarView) {
		_tabBarView = tabbarView;
		_tabBarView.delegate = self;
	}
    
    [self updateTabBar];
    [self updateContainerViewFrame];
}

- (void)updateTabBar{
    CGRect frame = _tabBarView.frame;
    frame.origin = CGPointZero;
    _tabBarView.frame = frame;
    [self.tabBar addSubview:_tabBarView];
    
    float offset = _tabBarView.frame.size.height - self.tabBar.frame.size.height;
    frame = self.tabBar.frame;
    frame.origin.y -= offset;
    frame.size.height += offset;
    self.tabBar.frame = frame;
}

- (void)updateContainerViewFrame{
    UIView *mainContainer = (UIView *)[self.view.subviews objectAtIndex:0];
    CGRect containerFrame = mainContainer.frame;
    if (self.tabBarHide) {
        containerFrame.size.height = self.view.frame.size.height;
    }
    else{
        containerFrame.size.height = self.view.frame.size.height - self.tabBar.frame.size.height;
    }
    mainContainer.frame = containerFrame;
}

- (void)setTabBarHide:(BOOL)hide {    
    CGRect tabFrame = self.tabBar.frame;
    if (hide) {
        tabFrame.origin.y = self.view.frame.size.height;
    }
    else{
        tabFrame.origin.y = self.view.frame.size.height - self.tabBar.frame.size.height;
    }
    self.tabBar.frame = tabFrame;

    [self updateContainerViewFrame];
}

- (BOOL)tabBarHide{
    return (self.tabBar.frame.origin.y ==  self.view.frame.size.height);
}

- (void)setTabBarHide:(BOOL)hide animated:(BOOL)animated{
    if (!animated) {
        self.tabBarHide = hide;
    }
    else{
        [UIView animateWithDuration:0.3
                         animations:^{
                             self.tabBarHide = hide;
                         }];
    }
}

- (void)setBadgeValue:(NSString *)value forIndex:(NSInteger)index{
    if ([value integerValue] <= 0) {
        [self.tabBarView setBadgeValue:@"" forIndex:index];
    }
    else{
        [self.tabBarView setBadgeValue:value forIndex:index];
    }
}

- (NSString *)badgeValueForIndex:(NSInteger)index{
    NSString *value = [self.tabBarView badgeValueForIndex:index];
    if ([value integerValue] <= 0) {
        return @"";
    }
    
    return value;
}


- (void)setSelectedIndex:(NSUInteger)index{
    BOOL sameVc = self.selectedIndex == index;
    if (sameVc) {
        if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) {
            [(UINavigationController *)self.selectedViewController popToRootViewControllerAnimated:YES];
        }
    }
    
	[super setSelectedIndex:index];
    if (_tabBarView.selectedIndex != index) {
        self.innerSet = YES;
        _tabBarView.selectedIndex = index;
        self.innerSet = NO;
    }
}


#pragma mark - BWXSegmentBarDelegate
- (void)segmentBar:(SimSegmentBar*)bar didSelectIndex:(NSInteger)index preIndex:(NSInteger)preIndex{
    if (!self.innerSet) {
        self.selectedIndex = index;
    }
}


@end
